﻿using System;
using System.Collections.Generic;
using System.Linq;
using AuchanScan.Models;

namespace AuchanScan.Datas
{
    public class TrophyHelper
    {
        private static TrophyHelper _current;
        private object _sync = new object();

        public TrophyHelper()
        {
            AllTrophies = new List<TrophyBase>();
            Initialize();
        }

        /// <summary>
        /// Gets the current instance of the cache
        /// </summary>
        public static TrophyHelper Current
        {
            get { return _current ?? (_current = new TrophyHelper()); }
        }

        public List<TrophyBase> AllTrophies { get; private set; }

        private void Initialize()
        {
            #region t1

            var t1 = new Trophy<List<Achat>>();
            t1.Id = 1;
            t1.Name = "Acheteur compulsif";
            t1.Description = "Vous avez acheté 10 fois le même produit";
            t1.IsRaised = achats =>
                {
                    if (achats != null && achats.Count > 0)
                    {
                        return achats.FirstOrDefault(n => n.Quantity > 10) != null;
                    }
                    return false;
                };

            #endregion

            #region t2

            var t2 = new Trophy<List<Achat>>();
            t2.Id = 2;
            t2.Name = "Radin";
            t2.Description = "Vous n'avez acheté qu'un produit :'(";
            t2.IsRaised = achats =>
                {
                    if (achats != null && achats.Count == 1)
                    {
                        return true;
                    }
                    return false;
                };

            #endregion

            #region t3

            var t3 = new Trophy();
            t3.Id = 3;
            t3.Name = "Jour béni !";
            t3.Description = "Vous avez acheté un jeudi, vous êtes bénis !";
            t3.IsRaised = () => { return DateTime.Now.DayOfWeek == DayOfWeek.Thursday; };

            #endregion

            #region t4

            var t4 = new Trophy<List<Achat>>();
            t4.Id = 4;
            t4.Name = "Soirée à venir ?";
            t4.Description = "Vous avez besoin de rester très éveillé? :D";
            t4.IsRaised =
                achats => { return achats.FirstOrDefault(a => a.Quantity > 2 && a.Product.Id == 1234) != null; };

            #endregion

            AllTrophies.Add(t1);
            AllTrophies.Add(t2);
            AllTrophies.Add(t3);
            AllTrophies.Add(t4);
        }

        public List<TrophyResult> CheckTrophies(List<Achat> achats, bool simulated = true)
        {
            var result = new List<TrophyResult>();
            //var trophies = new List<TrophyBase>(AllTrophies);

            foreach (TrophyBase trophy in AllTrophies)
            {
                //bool hasBeenWon = false;

                if (trophy is Trophy)
                {
                    var t = trophy as Trophy;
                    if (t.IsRaised())
                    {
                        result.Add(new TrophyResult { Id = t.Id, WasRaised = true });
                        //hasBeenWon = true;
                    }
                }
                else if (trophy is Trophy<List<Achat>>)
                {
                    var t = trophy as Trophy<List<Achat>>;
                    if (t.IsRaised(achats))
                    {
                        result.Add(new TrophyResult { Id = t.Id, WasRaised = true });
                        //hasBeenWon = true;
                    }
                }
            }
            return result;
        }

        internal List<TrophyBase> GetTrophies(List<int> list)
        {
           
           return (from l in list select AllTrophies.First((t) => t.Id == l)).ToList();
        }
    }
}